package com.zq.zqmanager.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

@Service
public class HttpClientService {

    @Autowired
    private CloseableHttpClient httpClient;
    @Autowired
    private RequestConfig requestConfig;

    /**
     * 通过给的url地址，获取服务器数据
     * @param url 服务器地址
     * @param params 封装用户参数
     * @param charset 设定字符编码
     * @return
     */
    public String doGet(String url, Map<String,String> params, String charset) {
        if (StringUtils.isEmpty(charset)) {
            charset = "utf-8";
        }

        try {
            //判断是否有参数
            URIBuilder uri = new URIBuilder(url);
            if (params != null) {
//                url +="?";
//                for (Map.Entry<String,String> entry: params.entrySet()) {
//                    String key = entry.getKey();
//                    String value = entry.getValue();
//                    url += key +"="+value+"&";
//                }
//                url = url.substring(0,url.length() - 1);
                for (Map.Entry<String,String> entry: params.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    uri.setParameter(key,value);
                }

            }
            String result = "";
            HttpGet httpGet = new HttpGet(uri.build());
            httpGet.setConfig(requestConfig);
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(),charset);
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String doGet(String url, Map<String,String> params) {
            return doGet(url,params,null);
    }
    public String doGet(String url) {
        return doGet(url,null,null);
    }

    public String post(String url,String json,String encode){
        if(StringUtils.isBlank(encode)) {
            encode = "UTF-8";
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
        try {
            //设置编码
            StringEntity se = new StringEntity(json,encode);
            se.setContentEncoding(encode);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httpPost.setEntity(se);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            String result = "";
            if(response!=null&&response.getStatusLine().getStatusCode()== HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity,encode);
            }
            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


}